#include "file_manager.h"

int FileManager::saveToFile(string fileName, const vector<SmartCar> cars) {
    cout << "正在保存分配信息..." << endl;
    // 打开文件
    ofstream file(fileName);
    if (!file.is_open()) {
        cerr << "无法打开文件" << endl;
        return -1;
    }
    // 写入数据
    for (const auto& car : cars) {
        file << car.stuName << ",";
        file << car.stuId << ",";
        file << car.id << ",";
        file << car.chassisId << ",";
        file << car.chassisModel << ",";
        file << car.wheelBase << ",";
        file << car.trackWidth << ",";
        file << car.groundClearance << ",";
        file << car.turningRadius << ",";
        file << car.driveType << ",";
        file << car.maxRange << ",";
        file << car.tireCount << ",";
        file << car.tireModel << ",";
        file << car.tireSize << ",";
        file << car.agxCount << ",";
        file << car.agxModel << ",";
        file << car.aiPerformance << ",";
        file << car.cudaCores << ",";
        file << car.tensorCores << ",";
        file << car.gpuMemorySize << ",";
        file << car.memorySize << ",";
        file << car.cameraCount << ",";
        file << car.cameraModel << ",";
        file << car.cameraType << ",";
        file << car.resolutionWidth << ",";
        file << car.resolutionHeight << ",";
        file << car.rgbFrameRate << ",";
        file << car.fovWidth << ",";
        file << car.fovHeight << ",";
        file << car.depthFrameRate << ",";
        file << car.lidarCount << ",";
        file << car.lidarModel << ",";
        file << car.lidarChannelCount << ",";
        file << car.lidarRange << ",";
        file << car.powerConsumption << ",";
        file << car.gyroscopeCount << ",";
        file << car.gyroscopeModel << ",";
        file << car.gyroscopeVendor << ",";
        file << car.displayCount << ",";
        file << car.displaySize << ",";
        file << car.displayModel << ",";
        file << car.batteryCount << ",";
        file << car.batteryParams << ",";
        file << car.supplyVoltage << ",";
        file << car.chargingTime << ",";  //共45项
        file << endl;
    }
    file.close();
    cout << "保存成功！！" << endl;
    m_fileName = fileName;
    return 1;
}

vector<SmartCar>& FileManager::getFileInfo() {
    if (m_smartCarsWithStu.empty()) {
        // 从csv文件中读入数据
        ifstream file(m_fileName);
        string line;
        while (getline(file, line)) {
            stringstream ss(line);
            string item;
            SmartCar car;

            vector<std::string> row;          // 用于存储分割后的数据
            while (getline(ss, item, ',')) {  // 使用逗号作为分隔符分割数据
                row.push_back(item);
            }
            car.stuName = row[0];
            car.stuId = row[1];
            car.id = row[2];
            car.chassisId = row[3];
            car.chassisModel = row[4];
            car.wheelBase = stoi(row[5]);
            car.trackWidth = stoi(row[6]);
            car.groundClearance = stoi(row[7]);
            car.turningRadius = stof(row[8]);
            car.driveType = row[9];
            car.maxRange = row[10];
            car.tireCount = stoi(row[11]);
            car.tireModel = row[12];
            car.tireSize = stoi(row[13]);
            car.agxCount = stoi(row[14]);
            car.agxModel = row[15];
            car.aiPerformance = row[16];
            car.cudaCores = stoi(row[17]);
            car.tensorCores = stoi(row[18]);
            car.gpuMemorySize = stoi(row[19]);
            car.memorySize = stoi(row[20]);
            car.cameraCount = stoi(row[21]);
            car.cameraModel = row[22];
            car.cameraType = row[23];
            car.resolutionWidth = stoi(row[24]);
            car.resolutionHeight = stoi(row[25]);
            car.rgbFrameRate = stoi(row[26]);
            car.fovWidth = stoi(row[27]);
            car.fovHeight = stoi(row[28]);
            car.depthFrameRate = stoi(row[29]);
            car.lidarCount = stoi(row[30]);
            car.lidarModel = row[31];
            car.lidarChannelCount = stoi(row[32]);
            car.lidarRange = stof(row[33]);
            car.powerConsumption = stof(row[34]);
            car.gyroscopeCount = stoi(row[35]);
            car.gyroscopeModel = row[36];
            car.gyroscopeVendor = row[37];
            car.displayCount = stoi(row[38]);
            car.displaySize = row[39];
            car.displayModel = row[40];
            car.batteryCount = stoi(row[41]);
            car.batteryParams = row[42];
            car.supplyVoltage = stof(row[43]);
            car.chargingTime = stof(row[44]);
            m_smartCarsWithStu.push_back(car);
        }
    }
    return m_smartCarsWithStu;
}
